import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': resolve(__dirname, './src'),
      '@components': resolve(__dirname, './src/components'),
      '@hooks': resolve(__dirname, './src/hooks'),
      '@services': resolve(__dirname, './src/services'),
      '@stores': resolve(__dirname, './src/stores'),
      '@utils': resolve(__dirname, './src/utils'),
      '@assets': resolve(__dirname, './src/assets'),
      '@data': resolve(__dirname, './src/data'),
    },
  },
  define: {
    global: 'globalThis',
  },
  optimizeDeps: {
    include: ['three', 'd3', 'gsap', 'howler', 'buffer']
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['react', 'react-dom'],
          animation: ['framer-motion', 'gsap', 'lottie-react'],
          three: ['three', '@react-three/fiber', '@react-three/drei'],
          d3: ['d3'],
          utils: ['axios', 'howler', 'konva', 'react-konva'],
          crypto: ['buffer']
        }
      }
    },
    target: 'esnext',
    minify: 'terser'
  },
  server: {
    port: 3000,
    open: true,
    cors: true,
    proxy: {
      '/api/webase': {
        target: 'http://localhost:5002',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/webase/, '/WeBASE-Front'),
        configure: (proxy, _options) => {
          proxy.on('error', (err, _req, _res) => {
            console.log('WeBASE代理错误:', err);
          });
          proxy.on('proxyReq', (proxyReq, req, _res) => {
            console.log('发送WeBASE代理请求:', req.method, req.url);
          });
          proxy.on('proxyRes', (proxyRes, req, _res) => {
            console.log('收到WeBASE代理响应:', proxyRes.statusCode, req.url);
          });
        }
      }
    }
  }
})
